package com.srwl.mytx.parse;


import com.srwl.mytx.domain.OrderReceiveInfo;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.utils.PreferenceManager;

import java.util.List;

public class UserProfileManager {

    private static UserProfileManager instance = null;

    /**
     * setupView flag: test if the sdk has been inited before, we don't need to setupView
     * again
     */


    public static UserProfileManager getInstance() {
        if (instance == null) {
            instance = new UserProfileManager();
        }

        return instance;
    }


    private User loginUser;


    private UserProfileManager() {
    }


    public synchronized void reset() {

        loginUser = null;
        PreferenceManager.getInstance().removeCurrentUserInfo();
    }

    public synchronized User getLoginUserInfo() {
        if (loginUser == null) {
            loginUser = new User();
            loginUser.setuId(getUserID());
            loginUser.setUsername(getUserName());
            loginUser.setNick(getUserNick());
            loginUser.setAvatar(getUserAvatar());
            loginUser.setPhone(getUserPhoneNumber());
            loginUser.setArea(getUserArea());
            loginUser.setGender(getUserGender());
            loginUser.setBalance(getUserBalance());
            loginUser.setAliUid(getUserAliUid());
            loginUser.setAliAccount(getLoginUserAliAccount());
            loginUser.setPayLimit(getPayLimit());
            loginUser.setDeposit(getDeposit());
            loginUser.setOrderReceiveInfoList(getOrderReceiveInfoList());
            loginUser.setLiveRoomSupperManager(PreferenceManager.getInstance().getLiveRoomSupperManager());
            loginUser.setType(getType());
            loginUser.setStatus(getStatus());
            loginUser.setToken(getUserToken());
        }
        return loginUser;
    }

    public void setUserName(String userName) {
        if (loginUser != null) {
            loginUser.setUsername(userName);
        }
        PreferenceManager.getInstance().setUserName(userName);
    }

    public String getUserName() {
        return PreferenceManager.getInstance().getUsername();
    }

    public String getUserNick() {
        return PreferenceManager.getInstance().getUserNick();
    }

    public void setUserNick(String nickname) {
        if (loginUser != null) {
            loginUser.setNick(nickname);
        }
        PreferenceManager.getInstance().setUserNick(nickname);
    }

    public String getUserAvatar() {
        return PreferenceManager.getInstance().getUserAvatar();
    }

    public void setUserAvatar(String url) {
        if (loginUser != null) {
            loginUser.setAvatar(url);
        }
        PreferenceManager.getInstance().setUserAvatar(url);
    }

    public String getUserArea() {
        return PreferenceManager.getInstance().getUserArea();
    }

    public void setUserArea(String area) {
        if (loginUser != null) {
            loginUser.setArea(area);
        }
        PreferenceManager.getInstance().setUserArea(area);
    }

    public String getUserID() {

        return PreferenceManager.getInstance().getUserId();
    }

    public void setUserID(String userID) {
        if (loginUser != null) {
            loginUser.setuId(userID);
        }
        PreferenceManager.getInstance().setUserID(userID);
    }

    public Integer getUserGender() {

        return PreferenceManager.getInstance().getUserGender();
    }

    public void setUserGender(Integer gender) {
        if (loginUser != null) {
            loginUser.setGender(gender);
        }
        PreferenceManager.getInstance().setUserGender(gender);
    }

    public void setUserBalance(double balance) {
        if (loginUser != null) {
            loginUser.setBalance(balance);
        }
        PreferenceManager.getInstance().setUserBalance(balance);
    }

    public double getUserBalance() {
        return PreferenceManager.getInstance().getUserBalance();
    }


    public void setDeposit(Double deposit) {
        if (loginUser != null) {
            loginUser.setDeposit(deposit);
        }
        PreferenceManager.getInstance().setUserDeposit(deposit);
    }

    public double getDeposit() {
        return PreferenceManager.getInstance().getUserDeposit();
    }

    public List<OrderReceiveInfo> getOrderReceiveInfoList() {
        return PreferenceManager.getInstance().getOrderReceiveInfoList();
    }

    public void setOrderReceiveInfoList(List<OrderReceiveInfo> orderReceiveInfoList) {
        if (loginUser != null) {
            loginUser.setOrderReceiveInfoList(orderReceiveInfoList);
        }
        PreferenceManager.getInstance().setOrderReceiveInfoList(orderReceiveInfoList);
    }

    public void setLiveRoomSupperManager(boolean liveRoomSupperManager) {
        if (loginUser != null) {
            loginUser.setLiveRoomSupperManager(liveRoomSupperManager);
        }
        PreferenceManager.getInstance().setLiveRoomSupperManager(liveRoomSupperManager);
    }


    public void setUserAliUid(String aliUid) {
        if (loginUser != null) {
            loginUser.setAliUid(aliUid);
        }
        PreferenceManager.getInstance().setCurrentUserAliUid(aliUid);
    }

    public void setLoginUserAliAccount(String aliAccount) {
        if (loginUser != null) {
            loginUser.setAliAccount(aliAccount);
        }
        PreferenceManager.getInstance().setCurrentUserAliAccount(aliAccount);
    }

    public String getLoginUserAliAccount() {
        return PreferenceManager.getInstance().getCurrentUserAliAccount();
    }

    public String getUserAliUid() {
        return PreferenceManager.getInstance().getCurrentUserAliUid();
    }

    public String getUserPhoneNumber() {
        return PreferenceManager.getInstance().getCurrentuserPhoneNumber();
    }

    public void setUserPhoneNumber(String phoneNum) {
        if (loginUser != null) {
            loginUser.setPhone(phoneNum);
        }
        PreferenceManager.getInstance().setCurrentuserPhoneNumber(phoneNum);
    }

    public void setUserToken(String token) {
        if (loginUser != null) {
            loginUser.setToken(token);
        }
        PreferenceManager.getInstance().setCurrentUserToken(token);
    }

    public String getUserToken() {
        return PreferenceManager.getInstance().getCurrentUserToken();
    }

    public void setWanTuToken(String wanTuToken) {
        PreferenceManager.getInstance().setWanTuToken(wanTuToken);
    }

    public String getWanTuToken() {
        return PreferenceManager.getInstance().getWanTuToken();
    }

    public void setPayLimit(int payLimit) {
        if (loginUser != null) {
            loginUser.setPayLimit(payLimit);
        }
        PreferenceManager.getInstance().setCurrentUserPayLimit(payLimit);
    }


    public Integer getPayLimit() {
        return PreferenceManager.getInstance().getCurrentUserPayLimit();
    }

    public void setType(int type) {
        if (loginUser != null) {
            loginUser.setType(type);
        }
        PreferenceManager.getInstance().setType(type);
    }

    public int getType() {
        return PreferenceManager.getInstance().getType();
    }


    public void setStatus(int status) {
        if (loginUser != null) {
            loginUser.setStatus(status);
        }
        PreferenceManager.getInstance().setStatus(status);
    }

    public int getStatus() {
        return PreferenceManager.getInstance().getStatus();
    }

    public void syncLoginUserInfo(User user) {
        if (user == null) {
            return;
        }
        if (user.getuId() != null) {
            setUserID(user.getuId());
        }
        if (user.getPhone() != null) {
            setUserPhoneNumber(user.getPhone());
        }

        if (user.getUsername() != null) {
            setUserName(user.getUsername());
        }

        if (user.getArea() != null) {
            setUserArea(user.getArea());
        }
        if (user.getNick() != null) {
            setUserNick(user.getNick());
        }
        if (user.getAvatar() != null) {
            setUserAvatar(user.getAvatar());
        }
        if (user.getGender() != null) {
            setUserGender(user.getGender());
        }
        if (user.getBalance() != null) {
            setUserBalance(user.getBalance());
        }
        if (user.getAliUid() != null) {
            setUserAliUid(user.getAliUid());
        }

        if (user.getAliAccount() != null) {
            setLoginUserAliAccount(user.getAliAccount());
        }

        if (user.getPayLimit() != null) {
            setPayLimit(user.getPayLimit());
        }

        if (user.getDeposit() != null) {
            setDeposit(user.getDeposit());
        }

        if (user.getOrderReceiveInfoList() != null) {
            setOrderReceiveInfoList(user.getOrderReceiveInfoList());
        }

        if (user.getLiveRoomSupperManager() != null) {
            setLiveRoomSupperManager(user.getLiveRoomSupperManager());
        }

        if (user.getType() != null) {
            setType(user.getType());
        }

        if (user.getStatus() != null) {
            setStatus(user.getStatus());
        }
        if (user.getToken() != null) {
            setUserToken(user.getToken());
        }
    }
}
